package smedi.ismedi.mobile.bff.controller.inmail;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.inmail.api.dto.request.*;
import com.smedi.ismedi.inmail.api.dto.response.*;
import com.smedi.ismedi.inmail.api.feign.RemoteOverviewFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import smedi.ismedi.mobile.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;

/**
 * 通知公告BFF
 *
 * @author jiangqinxuan
 * @date 2021/08/03
 */
@Slf4j
@Api(value = "安全统计", tags = {"安全统计"})
@RequestMapping(value = "/mobilebff/v1/overview")
@RestController
@RequiredArgsConstructor
public class OverviewController {
    private final RemoteOverviewFeignApi remoteOverviewFeignApi;

    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;

    /**
     * 项目统计
     *
     * @return ResponseData<ProjectStatisticsByRegionResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "项目统计")
    @PostMapping(value = "/project")
    ResponseData<ProjectOverviewPaginationData> getProjectOverviewPage(@Validated @RequestBody ProjectOverviewPageRequest projectOverviewPageRequest) {
        return remoteOverviewFeignApi.getProjectOverviewPage(projectOverviewPageRequest);
    }

    /**
     * 项目统计导出
     *
     */
    @ApiOperation(httpMethod = "POST", value = "项目统计导出")
    @PostMapping(value = "/project/export")
    ResponseData<Boolean> exportProjectOverview(@Validated @RequestBody ProjectOverviewListRequest projectOverviewListRequest, HttpServletResponse response) {
        String signKey = remoteOverviewFeignApi.exportProjectOverview(projectOverviewListRequest).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 安全检查（主体院）统计
     *
     * @return ResponseData<PaginationData < SafetyCheckOverviewResponse>>
     */
    @ApiOperation(httpMethod = "POST", value = "安全检查统计（主体院）")
    @PostMapping(value = "/safety-check/organization")
    ResponseData<SafetyCheckOrgOverviewPaginationData> getSafetyCheckOrganizationOverviewPage(@Validated @RequestBody SafetyCheckOrganizationOverviewPageRequest safetyCheckOrganizationOverviewPageRequest) {
        return remoteOverviewFeignApi.getSafetyCheckOrganizationOverviewPage(safetyCheckOrganizationOverviewPageRequest);
    }

    /**
     * 安全检查（主体院）统计导出
     *
     * @return url
     */
    @ApiOperation(httpMethod = "POST", value = "安全检查统计（主体院）导出")
    @PostMapping(value = "/safety-check/organization/export")
    ResponseData<Boolean> exportSafetyCheckOrganizationOverview(@Validated @RequestBody SafetyCheckOrganizationOverviewListRequest safetyCheckOrganizationOverviewListRequest, HttpServletResponse response) {
        String signKey = remoteOverviewFeignApi.exportSafetyCheckOrganizationOverview(safetyCheckOrganizationOverviewListRequest).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 安全检查（项目）统计
     *
     * @return ResponseData<PaginationData < SafetyCheckOverviewResponse>>
     */
    @ApiOperation(httpMethod = "POST", value = "安全检查统计（项目）")
    @PostMapping(value = "/safety-check/project")
    ResponseData<PaginationData<SafetyCheckProjectOverviewResponse>> getSafetyCheckProjectOverviewPage(@Validated @RequestBody SafetyCheckProjectOverviewPageRequest safetyCheckProjectOverviewPageRequest) {
        return remoteOverviewFeignApi.getSafetyCheckProjectOverviewPage(safetyCheckProjectOverviewPageRequest);
    }

    /**
     * 安全检查（项目）统计导出
     *
     * @return url
     */
    @ApiOperation(httpMethod = "POST", value = "安全检查统计（项目）导出")
    @PostMapping(value = "/safety-check/project/export")
    ResponseData<Boolean> exportSafetyCheckProjectOverview(@Validated @RequestBody SafetyCheckProjectOverviewListRequest safetyCheckProjectOverviewListRequest, HttpServletResponse response) {
        String signKey = remoteOverviewFeignApi.exportSafetyCheckProjectOverview(safetyCheckProjectOverviewListRequest).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 视频监控统计
     *
     * @return ResponseData<PaginationData<VideoMonitorOverviewResponse>>
     */
    @ApiOperation(httpMethod = "POST", value = "视频监控(主体院)统计")
    @PostMapping(value = "/video-monitor/organization")
    ResponseData<VideoMonitorOrgOverviewPaginationData> getVideoMonitorOrganizationOverviewPage(@Validated @RequestBody VideoMonitorOrganizationOverviewPageRequest videoMonitorOrganizationOverviewPageRequest) {
        return remoteOverviewFeignApi.getVideoMonitorOrganizationOverviewPage(videoMonitorOrganizationOverviewPageRequest);
    }

    /**
     * 视频监控统计导出
     *
     * @return url
     */
    @ApiOperation(httpMethod = "POST", value = "视频监控(主体院)统计导出")
    @PostMapping(value = "/video-monitor/organization/export")
    ResponseData<Boolean> exportVideoMonitorOrganizationOverview(@Validated @RequestBody VideoMonitorOrganizationOverviewListRequest videoMonitorOrganizationOverviewListRequest, HttpServletResponse response) {
        String signKey =  remoteOverviewFeignApi.exportVideoMonitorOrganizationOverview(videoMonitorOrganizationOverviewListRequest).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 视频监控统计
     *
     * @return ResponseData<PaginationData<VideoMonitorOverviewResponse>>
     */
    @ApiOperation(httpMethod = "POST", value = "视频监控(项目)统计")
    @PostMapping(value = "/video-monitor/project")
    ResponseData<PaginationData<VideoMonitorProjectOverviewResponse>> getVideoMonitorProjectOverviewPage(@Validated @RequestBody VideoMonitorProjectOverviewPageRequest videoMonitorProjectOverviewPageRequest){
        return remoteOverviewFeignApi.getVideoMonitorProjectOverviewPage(videoMonitorProjectOverviewPageRequest);
    }

    /**
     * 视频监控统计导出
     *
     * @return url
     */
    @ApiOperation(httpMethod = "POST", value = "视频监控(项目)统计导出")
    @PostMapping(value = "/video-monitor/project/export")
    ResponseData<Boolean> exportVideoMonitorProjectOverview(@Validated @RequestBody VideoMonitorProjectOverviewListRequest videoMonitorProjectOverviewListRequest, HttpServletResponse response) {
        String signKey =  remoteOverviewFeignApi.exportVideoMonitorProjectOverview(videoMonitorProjectOverviewListRequest).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 钉钉相关数据更新
     *
     * @return ResponseData<Boolean>
     */
    @ApiOperation(httpMethod = "POST", value = "钉钉相关数据更新")
    @PostMapping(value = "/ding-talk/data/update")
    ResponseData<Boolean> updateDingTalkData(){
        return remoteOverviewFeignApi.updateDingTalkData();
    }

    /**
     * 主体院考勤信息
     * */
    @ApiOperation(httpMethod = "POST" ,value = "主体院钉钉考勤统计")
    @PostMapping(value = "/ding-attendance/org/page")
    ResponseData<PaginationData<DingAttendanceOverViewResponse>> getDingAttendancePaginationDataResponseData(@Validated @RequestBody DingAttendanceOverViewPageRequest dingAttendanceOverViewPageRequest){
        return remoteOverviewFeignApi.getDingAttendancePaginationDataResponseData(dingAttendanceOverViewPageRequest);
    }

    /**
     * 项目考勤信息
     * */
    @ApiOperation(httpMethod = "POST",value = "项目钉钉考勤统计")
    @PostMapping(value = "/ding-attendance/project/page")
    ResponseData<PaginationData<DingAttendanceProjectOverViewResponse>> getDingAttendanceProjectPaginationDataResponseData(@Validated @RequestBody DingAttendanceProjectOverViewPageRequest dingAttendanceProjectOverViewPageRequest){
        return remoteOverviewFeignApi.getDingAttendanceProjectPaginationDataResponseData(dingAttendanceProjectOverViewPageRequest);
    }

    /**
     * 单个项目考勤信息详情
     * */
    @ApiOperation(httpMethod = "POST",value = "项目钉钉考勤详情")
    @PostMapping(value = "/ding-attendance/project")
    ResponseData<DingDingAttendanceProjectResponse> getProjectDetail(@Validated @RequestBody DingDingProjectRequest dingDingProjectRequest){
        return remoteOverviewFeignApi.getProjectDetail(dingDingProjectRequest);
    }

    /**
     * 主体院考勤信息导出
     * */
    @ApiOperation(httpMethod = "POST",value = "主体院钉钉考勤信息导出")
    @PostMapping(value = "/ding-attendance/org/export")
    ResponseData<Boolean> exportOrgAttendance(@RequestBody DingAttendanceOrgExportRequest request,HttpServletResponse response){
        String signKey = remoteOverviewFeignApi.exportOrgAttendance(request).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 项目钉钉考勤信息导出
     * */
    @ApiOperation(httpMethod = "POST",value = "项目钉钉考勤信息导出")
    @PostMapping(value = "/ding-attendance/project/export")
    ResponseData<Boolean> exportProjectAttendance(@RequestBody DingAttendanceProjectExportRequest request,HttpServletResponse response){
        String signKey = remoteOverviewFeignApi.exportProjectAttendance(request).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }
}
